package com.csdig.cms.dao.impl;

import java.util.List;

import com.csdig.cms.dao.ContentTxtDAO;
import com.csdig.cms.model.ContentTxt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.csdig.cms.dao.ContentDAO;
import com.csdig.cms.model.Content;
import com.csdig.cms.model.ext.ContentExt;
import com.csdig.db.DBUtils;
import com.csdig.db.model.Pagination;
import com.csdig.db.model.Sql;

@Repository
public class ContentDAOImpl implements ContentDAO {
    @Autowired
    private ContentTxtDAO contentTxtDAO;

	@Override
	public void add(Content vo) throws Exception {
		Sql sql = new Sql(
				"insert into content(content_id,channel_id,title,title_img,description,link,release_date,view_count,ups,downs)");
		sql.append("values(:content_id,:channel_id,:title,:title_img,:description,:link,now(),:view_count,:ups,:downs)");
		sql.set("content_id", vo.getContentId());
		sql.set("channel_id", vo.getChannelId());
		sql.set("title", vo.getTitle());
        sql.set("title_img", vo.getTitleImg());
        sql.set("description", vo.getDescription());
		sql.set("link", vo.getLink());
		// sql.set("release_date", vo.getReleaseDate());
		sql.set("view_count", vo.getViewCount());
		sql.set("ups", vo.getUps());
		sql.set("downs", vo.getDowns());
		DBUtils.exeUpdate(sql);
	}

	@Override
	public void delete(Integer contentId) throws Exception {
		String sql = "delete from content where content_id=?";
		DBUtils.exeUpdate(sql, new Object[] { contentId });
	}

	@Override
	public Pagination<Content> findByCondition(Content vo, int pageNo, int pageSize) throws Exception {
		Sql sql = new Sql("select * from content where 1=1 ");
		// /add condtion here
		return DBUtils.queryPagination(sql, null, pageNo, pageSize, Content.class);
	}

	@Override
	public List<Content> findByCondition(Content vo) throws Exception {
		Sql sql = new Sql("select * from content where 1=1 ");
		// /add condtion here
		return DBUtils.queryBeanList(sql, Content.class);
	}

	@Override
	public Content findById(java.lang.Integer id) throws Exception {
		String sql = "select * from content where content_id=?";
		Content content = DBUtils.queryBean(sql, Content.class, id);
        ContentTxt txt = contentTxtDAO.findbyContentId(content.getContentId());
        content.setTxt(txt);
        return content;
	}

	@Override
	public List<Content> listAll() throws Exception {
		return findByCondition(null);
	}

	@Override
	public void update(Content vo) throws Exception {
		String sql = "update content set channel_id=?,title=?,description=?,link=?,release_date=?,view_count=?,ups=?,downs=?, title_img=?  where content_id=?";
		DBUtils.exeUpdate(sql,
				new Object[] { vo.getChannelId(), vo.getTitle(), vo.getDescription(), vo.getLink(),
						vo.getReleaseDate(), vo.getViewCount(), vo.getUps(), vo.getDowns(), vo.getTitleImg(), vo.getContentId() });
	}

	@Override
	public Pagination<Content> findByCondition(List<Integer> channelIds, int pageNo, int pageSize) throws Exception {
		Sql sql = new Sql("select * from content where 1=1 ");
		sql.append(" and channel_id in(");
		for (int i = 0; i < channelIds.size(); i++) {
			if (i == channelIds.size() - 1) {
				sql.append(channelIds.get(i) + "");
			} else {
				sql.append(channelIds.get(i) + ",");
			}
		}
		sql.append(")");
		return DBUtils.queryPagination(sql, null, pageNo, pageSize, Content.class);
	}

	@Override
	public List<Content> findByCondition(String[] fieldNames, Object[] values) throws Exception {
		Sql sql = new Sql("select * from content where 1=1");
		for (int i = 0; i < fieldNames.length; i++) {
			String field = fieldNames[i];
			sql.append(" and " + field + "=:" + field);
			sql.set(field, values[i]);
		}
		return DBUtils.queryBeanList(sql, Content.class);
	}

	@Override
	public List<ContentExt> findListForTag(Integer channelId, int first, int count, String orderBy) throws Exception {
		Sql sql = new Sql("select * from content where 1=1 ");
		sql.append(" and channel_id=:channel_id ");
		sql.set("channel_id", channelId);
		if (StringUtils.isNotEmpty(orderBy))
			sql.append(orderBy);
		sql.append(" limit " + first + "," + count);
		return DBUtils.queryBeanList(sql, ContentExt.class);
	}

	@Override
	public Pagination<ContentExt> findPagForTag(Integer channelId, int pageNo, int defCount, String orderBy)
			throws Exception {
		Sql sql = new Sql("select * from content where 1=1 ");
		sql.append(" and channel_id=:channel_id ");
		sql.set("channel_id", channelId);
		if (StringUtils.isNotEmpty(orderBy))
			sql.append(orderBy);
		return DBUtils.queryPagination(sql, pageNo, defCount, ContentExt.class);
	}

}
